﻿/**************************************************************************
创建日期:	2019/7/13 22:25:51     
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com
创建时间:	
Copyright (c)    ZhCun.cn

描	述：
记	录：
***************************************************************************/
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
using ZhCun.WebCommon.AL;

namespace ZhCun.WebCommon.WebPage
{    
    [CommonActionFilter]
    public class PageController: Controller
    {
        string _RemoteIp;
        /// <summary>
        /// 返回远程请求地址
        /// </summary>
        protected string RemoteIp
        {
            get
            {
                if (_RemoteIp == null)
                {
                    _RemoteIp = Request.HttpContext.GetRemoteIp();
                }
                return _RemoteIp;
            }
        }
        /// <summary>
        /// 创建AL对象，并复制LoginHost
        /// </summary>
        protected virtual TAL CreateALObject<TAL>()
            where TAL : BaseAL, new()
        {
            TAL al = new TAL();
            al.SetLoginHost(RemoteIp);
            return al;
        }
        /// <summary>
        /// 获取远程Ip地址
        /// </summary>
        [NonAction]
        protected string GetRemoteIp()
        {
            return Request.HttpContext.GetRemoteIp();
        }
        /// <summary>
        /// 获取请求跟地址
        /// </summary>
        [NonAction]
        protected string GetRequestRootHost()
        {
            return HttpContext.GetRootHost();
        }
    }

    public class PageController<TAL> : PageController
        where TAL : BaseAL, new()
    {
        TAL _AL;
        /// <summary>
        /// 当前控制器所依赖的AL对象
        /// </summary>
        public TAL ALObj
        {
            get
            {
                if (_AL == null)
                {
                    _AL = CreateALObject<TAL>();
                }
                return _AL;
            }
        }
    }
}
